import os
import time,datetime
if __name__ == '__main__':
    os.environ.setdefault( "DJANGO_SETTINGS_MODULE", "analyse.settings")
    import django
    django.setup()

    from testdemo import models
# print(time.time())
# print (time.strftime('%Y-%m-%d',time.localtime(time.time())))
# print (time.strftime('%Y-%m-%d',time.localtime(time.time()-2592000)))
# print (time.strftime('%Y-%m-%d',time.localtime(time.time()-604800)))
# str_p ="2019-5-10"
# dateTime_p = datetime.datetime.strptime(str_p,'%Y-%m-%d')
# print(dateTime_p)
# print(type(datetime))
# time1 =time.strftime('%Y-%m-%d',time.localtime(time.time()-86400))
# time2 = time.strftime('%Y-%m-%d',time.localtime(time.time()-2592000))
time3 = time.strftime('%Y-%m-%d',time.localtime(time.time()-604800))
# print(time2)
# ret2 = models.T_newsTables.objects.filter(post_date__lt=time2)
# ret3 = models.T_newsTables.objects.filter(post_date__lt=time3)
# print(ret2)
# print(ret3)
# print(time1)
# ret1 = models.T_newsTables.objects.filter(post_date=time1)
# ret2 = models.T_newsTables.objects.filter(post_date__gt=time2)
ret3 = models.T_newsTables.objects.filter(post_date__gt=time3)
# print(ret1)
# print(ret2)
print(ret3[1].post_date)
# print(ret3)



# ret1 = models.T_newsTables.objects.all().order_by('post_date')

# print(ret1)
#
# ret2 = models.T_newsTables.objects.all().order_by('-post_date')
#
# print(ret2)






















# ret = models.T_newsTables.objects.all().filter(emotion = 0)
# print(ret)




# ret = models.T_newsTables.objects.values_list('emotion')
# # print(ret)
# for r in ret :
#     # print(r[0])
#      if r[0] == 0 :
#         result = models.T_newsTables.objects.get(emotion=r)
#         print(result.headline)

# ret1 = models.T_newsTables.objects.filter(emotion=0).values_list()
# ret1_list = [result[1] for result in ret1]

# print(ret1)

# ret = models.T_newsTables.objects.values_list("emotion")
# print(ret)
# ret = models.T_newsTables.objects.get("emotion")
# print(ret)
# ret = models.Name.objects.get(name= "张三")
# print(ret)
# ret_list = models.Book.objects.filter(id__gt=1,id__lt=4)
# print(ret_list)
# ret_list = models.Book.objects.filter(id__gt=1)
#
# print(ret_list)
# print("这是分割线".center(30,'-'))
# ret = models.Person.objects.values_list('name','age','birthday')
# print(ret)#直接取到值放在一个元组中，组成一个列表
# print("这是分割线".center(30,'-'))
# print(ret[2])
# ret = models.Person.objects.get(id=1)
# print(ret)
# ret = models.Person.objects.exclude(id=2)
# print(ret)
# ret = models.Person.objects.values('name','age','birthday')
# print(ret)#取到的值是一个字典，一个属性名对应一个值
# print("分隔符".center(80,'*'))
# ret = models.Person.objects.all().order_by("birthday")
# print(ret)#按一定顺序排序
# print("分隔符".center(80,'*'))
# ret = models.Person.objects.all().order_by("birthday").reverse()
# print(ret)#将已经排序 的queryset数据顺序颠倒
# ret = models.Person.objects.first()
# print(ret)
# ret = models.Person.objects.exists()
# print(ret)
# print("来一波更牛逼的操作".center(80,"*"))
# print("filter".center(90,'_'))
# ret = models.Person.objects.filter(id__in = [1,2,3])
# print(ret)#打印包含id=xxxx的元素
# ret = models.Person.objects.filter(name__contains = '张')
# print(ret)#模糊查询，查出带xx字的元素可以是多条
# ret = models.Person.objects.exclude(id__in = [1,2])
# print(ret)
# #查询出除了id=1和2的数据元素
# # ret = models.Person.objects.filter(id__range[1,3])
# # print(ret)我想打印出从以到三的数据
# print("日期字段".center(80,"*"))
# ret = models.Person.objects.filter(birthday__month = 5 )
# print(ret)
# #可以按年月日进行模糊查询
# print("还有忽略大小写的操作，待完善")
#
#
# print("正向查询")
# #基于对象的跨表查询，拿到的数据是一个具体的对象
# name_obj = models.Name_1.objects.first()
# ret = name_obj.book#和本name相关联的book可以用点来取值
# print(ret)
# print("======")
# print(ret.id)
# #基于双下滑线的跨表查询
# ret = models.Name_1.objects.values("book__book_name")
# print(ret)#用双下划线代表跨表查询,拿到的数据是queryset类型的数据
# print("-----------")
# #反向查询1.0
# # book_obj = models.Book_1.objects.first()
# # name = book_obj.name_1_set.all()
# # print(name)
# # #2.0
# book_obj = models.Book_1.objects.first()#得到的是一个具体的对象，可以用点来调用具体的属性
# name = book_obj.names.all()
# print(name)
#
# print("多对多的相关操作".center(80,"-"))
#
# name_obj = models.Name.objects.first()
# ret = name_obj.books.all()
# print(ret)
# name_obj.books.create(book_name="张三自传")
# print(ret)#在张三名下再创建一本书，实现跨表添加
# # 一共做了两件事情1. 在书的表中创建了一本书。2.在书和作者的关系表中添加关联记录
#
#
# print("add用法".center(80,"*"))
# # add的用法，给一个作者添加一本数据表中已存在的书。添加关联
# # 在张三已经关联的书中在加一本id=7的书
# book_obj = models.Book.objects.get(id=7)
# name_obj.books.add(book_obj)
# print(ret)
#
#
# # 一次性添加多个对象
# books_objs = models.Book.objects.filter(id__in =[8,9])
# name_obj = models.Name.objects.get(id =4)
# name_obj.books.add(*books_objs)
# ret = name_obj.books.all()
# print(ret)
#
# # 直接添加id来关联
#
# name_obj = models.Name.objects.filter(id= 2)
# name_obj.books.add(9)
#
# print("remove删除name关联的一本书".center(80,"*"))
# book_obj = models.Book.objects.get(book_name= "跟潘老师学查百度")
# name_obj = models.Name.objects.get(id=4)
# name_obj.books.remove(book_obj)
# ret = name_obj.books.all()
# print(ret)
# print("清空")
# # 将name中id=3所关联的书全部清除
# name_obj = models.Name.objects.get(id=3)
# name_obj.books.clear()
#
#
# #集合查询
# from django.db.models import Avg,Sum,Max,Min,Count
# # 导入django内置的一些方法,求所有书价格的出平均值，和，最小值，最大值
# ret = models.Book.objects.all().aggregate(平均价格=Avg("money"),
#                                             最便宜的书=Min("money"),
#                                             最价格之和=Sum("money"),
#                                             最贵的书=Max("money"))
# #
# ret_1 = models.Book.objects.all().aggregate(平均价格=Avg("money"),)
# ret_2 = models.Book.objects.all().aggregate(最价格之和=Sum("money"))
# ret_3 = models.Book.objects.all().aggregate(最贵的书=Max("money"))
# ret_4 = models.Book.objects.all().aggregate(最便宜的书=Min("money"))
# print(ret_1,ret_2,ret_3,ret_4)
# print(ret)
#
# # #可以用以上两种形式对数据进行简单的操作
# print(ret.get("平均价格"))#可以用这种方法获得其中一个运算
#
# # #分组
# ret = models.Name.objects.all().annotate(book_name = Count("books"))#表名
# #拿到的是一个queryset对象，就可以进行filter的相关操作
# for name in ret:
#     print(name,"写了",name.book_name,"本书")
# #打印作品在一个以上的作者
# ret = models.Name.objects.all().annotate(book_name = Count("books"))#表名
# r=ret.filter(book_name__gt = 1)
# print(r)
#
# # 求出一个作者书的总价格
# rets = models.Name.objects.all().annotate(money_sum =Sum("books__money")).values_list("name",'money_sum')
# retss = models.Name.objects.all().annotate(money_sum =Sum("books__money"))
#
# for ret in retss:
#
#     print(ret,ret.name,ret.money_sum)
#
#
# # F和Q查询
# # F查询是对象与对象之间的操作
# # Q查询是解决filter的条件是（或）的关系
# # 要导入一些的模块
# #查出库存大于销量的图书
# from django.db.models import F,Q
# ret = models.Book.objects.filter(kucun__gt = F("xiaoliang"))
# print(ret)
# print("这是分割线".center(80,"*"))
# #特注释，具体的对象没有upset的用法
# #吧每一本书的销量增加原来的三倍
# models.Book.objects.update(xiaoliang = F("xiaoliang")*2)
# #以上的操作还可以进行一些加减乘除的相关运算
# #还可以进行一些在对象后面加一些新的字符串
# #导入相关的模块
# from django.db.models.functions import Concat
# from django.db.models import Value
# models.Book.objects.update(book_name =Concat(F("book_name"),Value("第一版")))
# #Q查询
# #Q语言查询更加的灵活
# #查询出库存大于500或者售价大于三十块钱的书
# rets = models.Book.objects.filter(Q(kucun__gt=500)|Q(money__gt=30))
# for ret in rets:
#     print(ret)
#
# print("这是分割线".center(80,"*"))
# #查询出库存大于500或者售价大于三十块钱，并且销量大于100
# rets = models.Book.objects.filter(Q(kucun__gt=500)|Q(money__gt=30),xiaoliang__gt = 100)
# for ret in rets:
#     print(ret)
#
#
